package de.islanders.persistence;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import de.islanders.unit.player.Player;
import de.islanders.unit.player.PlayerStatus;

public class PlayerStatusDAO {
	
	private EntityManager entityManager;
	
	public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    protected EntityManager getEntityManager() {
        if (entityManager == null)
            throw new IllegalStateException(
                    "EntityManager has not been set on DAO before usage");
        return entityManager;
    }
    
    public PlayerStatus findById(Integer id){
    	return entityManager.find(PlayerStatus.class, id);
    }
    
    public PlayerStatus findPlayerStatus(Player player){
    	Query query = entityManager.createQuery("SELECT p FROM PlayerStatus p WHERE player = :player");
    	query.setParameter("player", player);
    	return (PlayerStatus) query.getSingleResult();
    }
    
    public void makePersistent(PlayerStatus status){
    	entityManager.getTransaction().begin();
    	entityManager.persist(status);
    	entityManager.getTransaction().commit();
    }
}
